package com.campus.backend.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.backend.bean.feedback.FeedbackHandleBean;
import com.campus.backend.bean.feedback.FeedbackQueryParam;
import com.campus.backend.entity.Feedback;
import com.campus.backend.entity.FeedbackType;
import com.campus.backend.mapper.FeedbackMapper;
import com.campus.backend.vo.feedback.FeedbackVo;
import com.campus.common.bean.common.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 意见反馈表 服务实现类
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-06-06
 */
@Service
public class FeedbackService extends ServiceImpl<FeedbackMapper, Feedback> {

    @Autowired
    private FeedbackTypeService feedbackTypeService;

    /**
     * 分页查询反馈列表
     *
     * @param param 查询参数
     * @return 分页结果
     */
    public PageInfo<FeedbackVo> getFeedbackListPage(FeedbackQueryParam param) {
        PageInfo<Feedback> pageInfo = PageHelper.startPage(param.getPageNo(), param.getPageSize())
                .doSelectPageInfo(() -> this.getFeedbackDataList(param));

        // 获取所有反馈类型
        List<FeedbackType> feedbackTypeList = feedbackTypeService.list();
        Map<Long, String> typeNameMap = feedbackTypeList.stream()
                .collect(Collectors.toMap(type -> type.getId().longValue(), FeedbackType::getName));
        
        // 转换为VO对象
        List<FeedbackVo> voList = pageInfo.getList().stream().map(feedback -> {
            FeedbackVo vo = new FeedbackVo();
            BeanUtil.copyProperties(feedback, vo);
            
            // 设置用户类型名称
            if (feedback.getUserType() != null) {
                switch (feedback.getUserType()) {
                    case 1:
                        vo.setUserTypeName("顾客");
                        break;
                    case 2:
                        vo.setUserTypeName("商户");
                        break;
                    case 3:
                        vo.setUserTypeName("配送员");
                        break;
                    default:
                        vo.setUserTypeName("未知");
                        break;
                }
            }
            
            // 设置反馈类型名称
            if (feedback.getTypeId() != null) {
                vo.setTypeName(typeNameMap.get(feedback.getTypeId()));
            }
            
            // 设置状态名称
            if (feedback.getStatus() != null) {
                vo.setStatusName(feedback.getStatus()==1 ? "已处理" : "未处理");
            }
            
            return vo;
        }).collect(Collectors.toList());
        
        return PageUtil.convertPageInfo(pageInfo, voList);
    }

    private List<Feedback> getFeedbackDataList(FeedbackQueryParam param) {
        LambdaQueryWrapper<Feedback> queryWrapper = new LambdaQueryWrapper<>();
        
        // 用户ID
        if (param.getUserId() != null) {
            queryWrapper.eq(Feedback::getUserId, param.getUserId());
        }
        
        // 用户类型
        if (param.getUserType() != null) {
            queryWrapper.eq(Feedback::getUserType, param.getUserType());
        }
        
        // 反馈类型ID
        if (param.getTypeId() != null) {
            queryWrapper.eq(Feedback::getTypeId, param.getTypeId());
        }
        
        // 状态
        if (param.getStatus() != null) {
            queryWrapper.eq(Feedback::getStatus, param.getStatus());
        }
        
        // 联系方式模糊查询
        if (StrUtil.isNotBlank(param.getContact())) {
            queryWrapper.like(Feedback::getContact, param.getContact());
        }
        
        // 按创建时间倒序
        queryWrapper.orderByDesc(Feedback::getCreateTime);
        
        return this.list(queryWrapper);
    }
    
    /**
     * 处理反馈
     *
     * @param handleBean 处理参数
     */
    public void handleFeedback(FeedbackHandleBean handleBean) {
        Feedback feedback = this.getById(handleBean.getFeedbackId());
        if (feedback == null) {
            throw new RuntimeException("反馈不存在");
        }
        
        feedback.setStatus(1); // 设置为已处理
        feedback.setHandleRemark(handleBean.getHandleRemark());
        feedback.setHandelTime(LocalDateTime.now());
        
        this.updateById(feedback);
    }
    
    /**
     * 根据ID获取反馈详情
     *
     * @param feedbackId 反馈ID
     * @return 反馈详情
     */
    public FeedbackVo getFeedbackById(Integer feedbackId) {
        Feedback feedback = this.getById(feedbackId);
        if (feedback == null) {
            throw new RuntimeException("反馈不存在");
        }
        
        FeedbackVo vo = new FeedbackVo();
        BeanUtil.copyProperties(feedback, vo);
        
        // 设置用户类型名称
        if (feedback.getUserType() != null) {
            switch (feedback.getUserType()) {
                case 1:
                    vo.setUserTypeName("顾客");
                    break;
                case 2:
                    vo.setUserTypeName("商户");
                    break;
                case 3:
                    vo.setUserTypeName("配送员");
                    break;
                default:
                    vo.setUserTypeName("未知");
                    break;
            }
        }
        
        // 设置反馈类型名称
        if (feedback.getTypeId() != null) {
            FeedbackType feedbackType = feedbackTypeService.getById(feedback.getTypeId());
            if (feedbackType != null) {
                vo.setTypeName(feedbackType.getName());
            }
        }
        
        // 设置状态名称
        if (feedback.getStatus() != null) {
            vo.setStatusName(feedback.getStatus() == 1 ? "已处理" : "未处理");
        }
        
        return vo;
    }
    
    /**
     * 删除反馈
     *
     * @param feedbackId 反馈ID
     */
    public void deleteFeedback(Integer feedbackId) {
        Feedback feedback = this.getById(feedbackId);
        if (feedback == null) {
            throw new RuntimeException("反馈不存在");
        }
        
        this.removeById(feedbackId);
    }
}
